// initializers/database.go
/*
 * @Author Malred
 * @Date 2025-06-15 21:24:25
 * @Description 连接到数据库, 对外提供数据库实例
 */
package initializers

import (
	"log"
	"os"

	"gorm.io/driver/mysql"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

var DB *gorm.DB

func ConnectToDB() {
	//ConnectToMysql()
	ConnectToSqlite()
}

func ConnectToMysql() {
	var err error
	dsn := os.Getenv("MYSQL_DB_URL")
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal("Failed to connect to database")
	}
}

func ConnectToSqlite() {
	var err error
	dbPath := os.Getenv("SQLITE_MDB_URL") // 通过环境变量指定 SQLite 数据库文件路径
	DB, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
	if err != nil {
		log.Fatal("Failed to connect to database")
	}
}
